#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
using namespace std;

int main()
{
    int N,meetingnum,meetingtime,nowtime,shuchu;
    int start[20]={ 24 },eend[20]={ 24 },length[20]={ 0 },juli[20] = {24};
    int index;

    while(cin>>N)
        {
            for(int j=0;j<N;j++)
            {
                cin>>meetingnum;
                nowtime = 23;

                for(int i=0;i<meetingnum;i++)
                    {
                        cin>>start[i]>>eend[i];
                        length[i] = start[i]-eend[i];
                        if (nowtime >= start[i])
                            {
                            nowtime = start[i];
                            index = i;
                            }
                    }

                meetingtime = 0;

                while(nowtime<24)
                {

                    nowtime = nowtime+length[index];
                    meetingtime = meetingtime+length[index];

                    for (int i=0;i<meetingnum;i++)
                    {
                        if (i!=index) & (start[i] > eend[index] )
                        {
                            juli[i] = start[i]-eend[index];
                            if (shuchu > juli[i])
                            {
                            shuchu  = juli[i];
                            index = i;
                            }
                        }

                    }

                }
            }
            cout<<meetingtime<<endl;

        }
    return 0;
}
